﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinRTFramework.FrameworkConfiguration;
using WinRTFramework.FrameworkCore;
using WinRTFramework.FrameworkCore.Managers;
using WinRTFramework.FrameworkCore.Services;
using WinRTFramework.Interfaces.Contracts.Sharing;
using WinRTFramework.Interfaces.Navigation;
using WinRTFramework.Interfaces.Navigation.ViewModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml.Controls;

namespace WinRTFramework
{
    public class WinRTShareActivationContainer : IServiceInjector
    {
        private IApplicationAdapter _applicationAdapter;
        private Frame _rootFrame;
        private IViewViewModelMapper _viewProvider;
        private readonly IIoCAdapter _iocAdapter;
        private NavigationManager _navigationManager;
        private SharingTargetManager _sharingTargetManager;

        public class ExtensionsContainer
        {
            public ISharingTargetViewModelTypeSelector SharingTargetViewModelTypeSelector { get; set; }
        }

        private WinRTShareActivationContainer(IApplicationAdapter applicationAdapter, Frame rootFrame, IViewViewModelMapper viewProvider, IIoCAdapter iocAdapter)
        {
            _applicationAdapter = applicationAdapter;
            _rootFrame = rootFrame;
            _viewProvider = viewProvider;
            _iocAdapter = iocAdapter;

            Extensions = new ExtensionsContainer();
        }

        public static WinRTShareActivationContainer CreateFrom(IApplicationAdapter applicationAdapter, Frame rootFrame, IViewViewModelMapper viewProvider, IIoCAdapter iocAdapter)
        {
            var container = new WinRTShareActivationContainer(applicationAdapter, rootFrame, viewProvider, iocAdapter);
            container.Initialize();
            return container;
        }

        private void Initialize()
        {
            NavigationService = new NavigationService(_rootFrame, _viewProvider);
            _navigationManager = NavigationManager.CreateFrom(_applicationAdapter, _rootFrame, _viewProvider, this, new NavigationManager.NavigationManagerOptions() { HandleResumeAndSuspend = false });
            _sharingTargetManager = new SharingTargetManager(NavigationService, _iocAdapter);
        }

        public void HandleShareActivation(ShareTargetActivatedEventArgs shareTargetActivatedEventArgs)
        {
            _sharingTargetManager.SharingTargetViewModelTypeSelector = Extensions.SharingTargetViewModelTypeSelector;
            _sharingTargetManager.HandleShareActivation(shareTargetActivatedEventArgs);
        }

        private INavigationService NavigationService { get; set; }
        public ExtensionsContainer Extensions { get; private set; }

        #region IServiceInjector
        public void InjectServicesIntoViewModel(object viewModel)
        {
            if (viewModel is IRequireNavigationService)
            {
                ((IRequireNavigationService)viewModel).NavigationService = NavigationService;
            }
        }
        #endregion
    }
}
